package editor
{
	import Classes.pi_events;
	import Classes.pi_images;
	
	import UIFree.ObjectEvent;
	import UIFree.uif_component;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;

	public class pi_photo extends pi_source {
		private var _rect			: pi_rect;
		private var _lc				: LoaderContext;
		private var _initRectSize	: Point;
		private var _mask			: uif_component;
		private var _toolbar		: pi_baseToolbar;
		private var _store			: Object; 
//		private var _image			: Bitmap;
		
		public function pi_photo(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:String='white') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			visible = false;
			addEvent(pi_events.SETPHOTORECT, doResetSize, app);
/*			removeChild(_source);
			_image 	= new Bitmap();
			addChild(_image);*/
			_lc 	= new LoaderContext(true);
			_rect 	= new pi_rect(this, 0, 0, 20, 20);
			function eUpdate(e: Event): void {
				(app as photo_inject)['editor'].cardBody.dispatchEvent(new ObjectEvent(pi_events.UPDATECARD, This, true));
			}
			_rect.addEvent(uif_component.POSITION, eUpdate);
//			_rect.addEventListener(uif_component.DRAG, l_resize);
			_rect.addEvent(uif_component.ROTATION, eUpdate);
			_rect.addEvent(uif_component.RESIZE, eUpdate);
			_mask = new uif_component(this, 0, 0, width, height, 'mask');
			_rect.mask = _mask;

			initToolbar();
			_store = new Object();
			
			addEvent(pi_events.STORE, doStore, app);
			addEvent(pi_events.RESTORE, doRestore, app);
			addEvent(pi_events.DELSTORE, doDelstore, app);
		}
		
		protected function doStore(e: ObjectEvent): void {
			var si: Object = _store[e.object];
			if (si) {
				if (si.restoring) return;
				else si.rphoto.unload();
			}
			_store[e.object] = {
				rmat	: rect.transform.matrix.clone(),
				irsize	: _initRectSize.clone(),
				rsize	: rect.RSize.clone(),
				rphoto 	: _source.duplicate()
			}
			e.stopPropagation();
		}
		
		protected function doDelstore(e: ObjectEvent): void {
			if (_store[e.object]) {
				_store[e.object].rphoto.unload();
				delete(_store[e.object]);
			}
			e.stopPropagation();
		}
		
		protected function doRestore(e: ObjectEvent): void {
			if (_store[e.object]) {
				var si: Object = _store[e.object];
				si.restoring = true;
				function doLoadPhoto(el: Event): void {
					resetSize(si.irsize);
					rect.transform.matrix = si.rmat;
					rect.applySize(si.rsize);
					delete(si.restoring);
				}
				oneEvent(Event.COMPLETE, doLoadPhoto, _source.contentLoaderInfo);
				si.rphoto.duplicate((app as photo_inject).photo);
				e.stopPropagation();
			}
		}
		
		protected function initToolbar(): void {
			_toolbar = new pi_baseToolbar(this, getSpace(true), '');
			swapChildren(_toolbar, _rect);
			new pi_toolbarButton(_toolbar, pi_images.c_flip_v, 'Отразить по вертикали', function(): void {
				app.dispatchEvent(new Event(pi_events.VFLIPRELEASE));
			}, 99);
			new pi_toolbarButton(_toolbar, pi_images.c_flip_h, 'Отразить по горизонтали', function(): void {
				app.dispatchEvent(new Event(pi_events.HFLIPRELEASE));
			}, 99);
			new pi_toolbarButton(_toolbar, pi_images.c_extend, '', function(e: Event): void {
				app.dispatchEvent(new ObjectEvent(pi_events.EXTENDRELEASE, e.target));
			}, 30);
			_toolbar.refreshButtons();
			_toolbar.visible = false;
		}
		
		override public function getInner(incx: Number=0, incy: Number=0, incw:Number=0, inch:Number=0): Rectangle {
			var rect: Rectangle = super.getInner();
			rect.y += 40;
			rect.height -= 40;
			return rect;
		}
		
		override protected function doLoad(e: Event): void {
			super.doLoad(e);
/*			var rect: Rectangle = EnterHere(_imageSize);
			if (_image.bitmapData) _image.bitmapData.dispose();
			_image.bitmapData = new BitmapData(rect.width, rect.height);
			_image.x = rect.x;
			_image.y = rect.y;
			var mat: Matrix = new Matrix();
			mat.scale(rect.width / _imageSize.x, rect.height / _imageSize.y);
			_image.bitmapData.draw(_source, mat, null, null, null, true);*/
			
			if (_initRectSize) resetSize(_initRectSize);
			_toolbar.slideShow();
//			_toolbar.fx('y', _toolbar.y, source.y - 40 - getSpace(true)).start();
		}
		
		protected function resetSize(a_initRectSize: Point): void {
			_initRectSize = a_initRectSize;
			var tRect: Point = _initRectSize.clone();
			tRect.x *= sourceScale;
			tRect.y *= sourceScale;
/*			_rect.x = (width  - tRect.x) / 2 + tRect.x / 2;
			_rect.y = (height - tRect.y) / 2 + tRect.y / 2;
*/			
			_rect.x = _source.x + (_source.width - tRect.x) / 2 + tRect.x / 2;
			_rect.y = _source.y + (_source.height - tRect.y) / 2 + tRect.y / 2;
//			new uif_Timer(20, function(e: TimerEvent): void {
				visible = source.contentLoaderInfo.bytesLoaded > 0;
				_rect.reset(tRect);
//			}, null, 1);
		}
		
		protected function doResetSize(e: ObjectEvent): void {
			resetSize(e.object as Point);
		}
		
		public function loadPhoto(url: String, initRectSize: Point): void {
			_source.load(new URLRequest(url), _lc);
			_initRectSize = initRectSize;
		}
		
		public function get rect(): pi_rect {
			return _rect;
		}
	}
}